package com.ctg.behavior.calc.impala.builder.userbehavior;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.impala.report.userbehavior.CtUSReport;
import com.ctg.behavior.common.CtgRowsData;
import com.ctg.behavior.common.consts.CtFieldConsts;
import com.ctg.behavior.common.usersequence.CtUSCalcData;
import com.ctg.behavior.common.usersequence.CtUSCalcEventData;
import com.ctg.behavior.utils.CtDateUtils;

@Component
public class CtUSCalcResultBuilder {

	public CtgRowsData<CtUSCalcData> doBuilder(CtgRowsData<CtUSCalcData> calcData, List<LinkedHashMap<String, Object>> calcResult, final CtUSReport report) {

		calcData = this.innerBuilder(calcData, calcResult, report);

		return calcData;
	}

	public CtgRowsData<CtUSCalcData> innerBuilder(CtgRowsData<CtUSCalcData> calcData, List<LinkedHashMap<String, Object>> calcResult, final CtUSReport report) {

		CtUSCalcData oneCalcData = new CtUSCalcData();
		calcData.getList().add(oneCalcData);
		calcData.setTotal(report.getTotal());
		// list中的事件开始与结束时间
		oneCalcData.setFromDate(CtDateUtils.dateToString(report.getCalcDate().getFromDate()));
		oneCalcData.setToDate(CtDateUtils.dateToString(report.getCalcDate().getToDate()));
		// 行数累加
		// TEMP ==> Total ()
		oneCalcData.setTotal(oneCalcData.getTotal() + calcResult.size());
		// From
		for (LinkedHashMap<String, Object> oneRow : calcResult) {
			CtUSCalcEventData oneEventData = new CtUSCalcEventData();
			oneCalcData.getEvents().add(oneEventData);

			// 主要属性信息
			{
				oneEventData.setEvent(String.valueOf(oneRow.get(CtFieldConsts.FIELD_KEY_EVENT)));

				Object tObj = oneRow.get(CtFieldConsts.FIELD_KEY_EVENT_TIME);
				Long eventTime = Long.valueOf(tObj == null ? "0" : tObj.toString());
				String fromDateTime = CtDateUtils.dateToString(new Date(eventTime), CtDateUtils.YYYY_MM_DD_HH_MM_SS_SSS);
				oneEventData.setTime(fromDateTime);

				oneEventData.setUserId(String.valueOf(oneRow.get(CtFieldConsts.FIELD_USER_ID)));
				// oneEventData.setDistinctId(String.valueOf(oneRow.get(CtFieldConsts.FIELD_DEVICE_ID)));
				oneEventData.setDeviceId(String.valueOf(oneRow.get(CtFieldConsts.FIELD_DEVICE_ID)));
			}

//			Set<Entry<String, Object>> entries = oneRow.entrySet();
//			// int outCounter = 0;
//			for (Entry<String, Object> entry : entries) {
//				oneEventData.getProperties().put(entry.getKey(), String.valueOf(entry.getValue()));
//			}
			for(String property : report.getProperties()) {
				oneEventData.getProperties().put(property, String.valueOf(oneRow.get(property)));
			}
		}
		// 基本请求参数
		if (0 == report.getReportIndex()) {
			calcData.setRequestId(report.getRequestId());
			calcData.setAppId(report.getAppId());
			calcData.setBookmarkId(report.getBookmarkId());

			calcData.setDataUpdateTime(CtDateUtils.dateToString(new Date()));
			calcData.setReportUpdateTime(CtDateUtils.dateToString(new Date()));
		}

		return calcData;
	}

}
